package com.dfsoft.schedule;

import com.dfsoft.config.ViewFileConfig;
import com.dfsoft.util.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.File;
import java.time.LocalDateTime;


//1.主要用于标记配置类，兼备Component的效果。
@Configuration
//2.开启定时任务
@EnableScheduling
@Slf4j
public class FileScheduleTask {

    @Autowired
    private ViewFileConfig viewFileConfig;

    //3.添加定时任务
//    @Scheduled(cron = "0/5 * * * * ?")
    //或直接指定时间间隔，例如：5秒
    //@Scheduled(fixedRate=5000)
    @Scheduled(cron = "${cache.clean.cron:0 0 3 * * ?}")
    private void clearTempFileTasks() {
        log.info("执行临时文件清楚定时任务时间: " + LocalDateTime.now());
        //定时删除文件(临时文件)
        FileUtils.deleteDirectory(viewFileConfig.getTempPath());
        log.info("执行临时文件清楚定时任务时间结束: " + LocalDateTime.now());
        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
    }

}